<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Modified Akima interpolation</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../interpolation.html" title="Chapter 13. Interpolation">
<link rel="prev" href="cubic_hermite.html" title="Cubic Hermite interpolation">
<link rel="next" href="pchip.html" title="PCHIP interpolation">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="cubic_hermite.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pchip.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.makima"></a><a class="link" href="makima.html" title="Modified Akima interpolation">Modified Akima interpolation</a>
</h2></div></div></div>
<h4>
<a name="math_toolkit.makima.h0"></a>
      <span class="phrase"><a name="math_toolkit.makima.synopsis"></a></span><a class="link" href="makima.html#math_toolkit.makima.synopsis">Synopsis</a>
    </h4>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">interpolators</span><span class="special">/</span><span class="identifier">makima</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span> <span class="special">{</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RandomAccessContainer</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">makima</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>

    <span class="keyword">using</span> <span class="identifier">Real</span> <span class="special">=</span> <span class="identifier">RandomAccessContainer</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">;</span>

    <span class="identifier">makima</span><span class="special">(</span><span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">abscissas</span><span class="special">,</span> <span class="identifier">RandomAccessContainer</span><span class="special">&amp;&amp;</span> <span class="identifier">ordinates</span><span class="special">,</span>
           <span class="identifier">Real</span> <span class="identifier">left_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span>
           <span class="identifier">Real</span> <span class="identifier">right_endpoint_derivative</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">Real</span><span class="special">&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">());</span>

    <span class="identifier">Real</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="identifier">Real</span> <span class="identifier">prime</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="keyword">void</span> <span class="identifier">push_back</span><span class="special">(</span><span class="identifier">Real</span> <span class="identifier">x</span><span class="special">,</span> <span class="identifier">Real</span> <span class="identifier">y</span><span class="special">);</span>

    <span class="keyword">friend</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span> <span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="keyword">const</span> <span class="identifier">makima</span> <span class="special">&amp;</span> <span class="identifier">m</span><span class="special">);</span>
<span class="special">};</span>

<span class="special">}</span> <span class="comment">// namespaces</span>
</pre>
<h4>
<a name="math_toolkit.makima.h1"></a>
      <span class="phrase"><a name="math_toolkit.makima.modified_akima_interpolation"></a></span><a class="link" href="makima.html#math_toolkit.makima.modified_akima_interpolation">Modified
      Akima Interpolation</a>
    </h4>
<p>
      The modified Akima interpolant takes non-equispaced data and interpolates between
      them via cubic Hermite polynomials whose slopes are chosen by a modification
      of a geometric construction proposed by <a href="https://doi.org/10.1145/321607.321609" target="_top">Akima</a>.
      The modification is given by <a href="https://blogs.mathworks.com/cleve/2019/04/29/makima-piecewise-cubic-interpolation/" target="_top">Cosmin
      Ionita</a> and agrees with Matlab's version. The interpolant is <span class="emphasis"><em>C</em></span><sup>1</sup> and
      evaluation has 𝑶(log(<span class="emphasis"><em>N</em></span>)) complexity. This is faster than
      barycentric rational interpolation, but also less smooth. An example usage
      is as follows:
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span> <span class="number">5</span><span class="special">,</span> <span class="number">9</span> <span class="special">,</span> <span class="number">12</span><span class="special">};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">y</span><span class="special">{</span><span class="number">8</span><span class="special">,</span><span class="number">17</span><span class="special">,</span> <span class="number">4</span><span class="special">,</span> <span class="special">-</span><span class="number">3</span><span class="special">};</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">interpolators</span><span class="special">::</span><span class="identifier">makima</span><span class="special">;</span>
<span class="keyword">auto</span> <span class="identifier">spline</span> <span class="special">=</span> <span class="identifier">makima</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">y</span><span class="special">));</span>
<span class="comment">// evaluate at a point:</span>
<span class="keyword">double</span> <span class="identifier">z</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
<span class="comment">// evaluate derivative at a point:</span>
<span class="keyword">double</span> <span class="identifier">zprime</span> <span class="special">=</span> <span class="identifier">spline</span><span class="special">.</span><span class="identifier">prime</span><span class="special">(</span><span class="number">3.4</span><span class="special">);</span>
</pre>
<p>
      Periodically, it is helpful to see what data the interpolator has, and the
      slopes it has chosen. This can be achieved via
    </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">spline</span> <span class="special">&lt;&lt;</span> <span class="string">"\n"</span><span class="special">;</span>
</pre>
<p>
      Note that the interpolator is pimpl'd, so that copying the class is cheap,
      and hence it can be shared between threads. (The call operator and <code class="computeroutput"><span class="special">.</span><span class="identifier">prime</span><span class="special">()</span></code>
      are threadsafe.)
    </p>
<p>
      One unique aspect of this interpolator is that it can be updated in constant
      time. Hence we can use <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span></code>
      to do real-time interpolation:
    </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">circular_buffer</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="special">...</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_x</span><span class="special">{</span><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">};</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">circular_buffer</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span> <span class="identifier">initial_y</span><span class="special">{</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">,</span><span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">};</span>
<span class="keyword">auto</span> <span class="identifier">circular_akima</span> <span class="special">=</span> <span class="identifier">makima</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_x</span><span class="special">),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">initial_y</span><span class="special">));</span>
<span class="comment">// interpolate via call operation:</span>
<span class="keyword">double</span> <span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_akima</span><span class="special">(</span><span class="number">3.5</span><span class="special">);</span>
<span class="comment">// add new data:</span>
<span class="identifier">circular_akima</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="number">5</span><span class="special">,</span> <span class="number">8</span><span class="special">);</span>
<span class="comment">// interpolat at 4.5:</span>
<span class="identifier">y</span> <span class="special">=</span> <span class="identifier">circular_akima</span><span class="special">(</span><span class="number">4.5</span><span class="special">);</span>
</pre>
<p>
      <span class="inlinemediaobject"><object type="image/svg+xml" data="../../graphs/makima_vs_cubic_b.svg"></object></span>
    </p>
<p>
      The modified Akima spline compared to the cubic <span class="emphasis"><em>B</em></span>-spline.
      The modified Akima spline oscillates less than the cubic spline, but has less
      smoothness and is not exact on quadratic polynomials.
    </p>
<h4>
<a name="math_toolkit.makima.h2"></a>
      <span class="phrase"><a name="math_toolkit.makima.complexity_and_performance"></a></span><a class="link" href="makima.html#math_toolkit.makima.complexity_and_performance">Complexity
      and Performance</a>
    </h4>
<p>
      The complexity and performance is identical to that of the cubic Hermite interpolator,
      since this object simply constructs derivatives and forwards the data to <code class="computeroutput"><span class="identifier">cubic_hermite</span><span class="special">.</span><span class="identifier">hpp</span></code>.
    </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="cubic_hermite.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../interpolation.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="pchip.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
